package de.tabuma.customer.view;

import de.tabuma.customer.*;
import de.tabuma.customer.entity.*;

import de.tabuma.lib.tabumaview;
import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author alefen
 */
public class listView extends tabumaview {
    
    public listView(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    /**
     * <code><pre>
     * ########################################
     * #                                      #
     * # --Costumer List--------------------- #
     * # | Name                      Remove | #
     * # | -------------------------------- | #
     * # | $Customer                 remove | #
     * # | $Customer                 remove | #
     * # | -Button--                        | #
     * # | | Add   |                        | #
     * # | ---------                        | #
     * # ------------------------------------ #
     * #                                      #
     * ########################################
     * </pre></code>
     * @param context
     * @param request
     * @param response
     * @param out
     */
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        List<Customer> cList = (List<Customer>) request.getAttribute(constants.listAttribute);

        String customerListName = "Name";
        String customerListRemove = "remove";
        String customerListNoEntry = "No entry";

        String customerListAdd = "Add";

        out.println("<div class=\"contentContent\">");
        out.println("<table class=\"table\">");
        out.println("<tr>");
        out.println("<td class=\"tableheader\">"+customerListName+"</td>");
        out.println("<td class=\"tableheader\"></td>");
        out.println("</tr>");

        String link1 = "";
        String link2 = "";
        Enumeration enu = this.getParameter().keys();
        boolean first = true;
        while(enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = request.getParameter(key);
            if(first) {
               link1 += "&amp;";
            }
            link1 += key+"="+value;
            link2 += "<input type=\"hidden\" name=\""+key+"\" value=\""+value+"\"/>\n";
        }

        for(int i=0;i<cList.size();i++) {
            Customer customer = cList.get(i);
            String name = customer.getName();
            out.println("<tr>");
            out.println("<td class=\"tablecontent\"><a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.paramName+"="+constants.ACTION_UPDATE+"&amp;"+constants.paramId+"="+customer.getId()+"\">"+name+"</a></td>");
            out.println("<td class=\"tablecontent\"><a href=\""+this.getServlet()+"?"+link1+"&amp;"+constants.paramName+"="+constants.ACTION_REMOVE+"&amp;"+constants.paramId+"="+customer.getId()+"\">"+customerListRemove+"</a></td>");
            out.println("</tr>");
        }
        if(cList.size()==0) {
            out.println("<tr>");
            out.println("<td class=\"tablecontent\" colspan=\"2\">"+customerListNoEntry+"</td>");
            out.println("</tr>");
        }
        out.println("</table>");
        out.println("<br/>");
        out.println("<form action=\""+this.getServlet()+"\">");
        
        out.println(link2);
        out.println("<input type=\"hidden\" name=\""+constants.paramName+"\" value=\""+constants.ACTION_CREATE+"\"/>");
        out.println("<input type=\"submit\" value=\"" + customerListAdd + "\" class=\"button\">");
        out.println("</form>");
        out.println("</div>");
    }

}
